from django.shortcuts import render

# Create your views here.
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_jwt.serializers import jwt_payload_handler, jwt_encode_handler

from libs.sinaweibopy3 import weiboApi
from oauth.models import OauthUser
from user.models import User
from user.utils import jwt_response_payload_handler

# class PlantView:
#     def ThirdLogin(name):
#         if name == "weibo":
#             return Weibo()
#         elif name == "dingding":
#             return DingDing()


class OauthWeibo(APIView):
    permission_classes = (AllowAny,)
    def post(self,request):
        type = request.data.get("type")
        print(type) #只是一个判断，这个可以判断调用哪个链接
        url = weiboApi.get_authorize_url()
        return Response({"code":200,"msg":"获取成功","data":{"url":url}})

class OauthWeiboCallback(APIView):
    # def perform_authentication(self, request):
    #     pass
    permission_classes = (AllowAny,)
    def post(self,request):
        code = request.data.get("code")
        # 1. 根据code 换取 access_token,,weibo_uid
        access_token,weibo_uid  = weiboApi.code_to_access_token(code)

        try:
            oauth_user = OauthUser.objects.get(uid=weibo_uid,oauth_type='1')
        except Exception as e:
            oauth_user = None
        if oauth_user:
            #说明绑定有信息了，直接可以登录
            #返回用户token信息，返回登录状态   #0代表已注册 1代表未注册
            user = oauth_user.user
            payload = jwt_payload_handler(user)
            token = jwt_encode_handler(payload)
            data = jwt_response_payload_handler(token, user)
            data['type'] = "0"

            return Response({"codo": 200, "msg": "登陆成功","data":data})
        else:
            #说明没绑定信息，返回前端weibo_uid，让前端显示输入username，password进行数据绑定
            return Response({"code":200,"msg":"请输入账户绑定信息","data":{"type":"1","uid":weibo_uid}})


class OauthWeiboBindUser(APIView):
    permission_classes = (AllowAny,)
    def post(self,request):
        #分为两种情况0，先判断数据的完整性 1，是账户已拥有，校验密码 2，未有该账户，直接把账户密码存入user表
        weibo_uid = request.data.get("weibo_uid")
        username = request.data.get("username")
        password = request.data.get("password")
        if not all([weibo_uid,username,password]):
            return Response({"code":400,"msg":"参数不全，请重新输入"})
        try:
            user = User.objects.get(username=username)
        except Exception as e:
            user = None
        if user:
            # 1，是账户已拥有，校验密码
            if user.check_password(password):
                ou=OauthUser(uid=weibo_uid,user=user,oauth_type='1')
                ou.save()
                payload = jwt_payload_handler(user)
                token = jwt_encode_handler(payload)
                data = jwt_response_payload_handler(token, user)
                data['type'] = "0" #指定账户已绑定
                return Response({"codo": 200, "msg": "登陆成功", "data": data})
            else:
                return Response({"code":400,"msg":"账户或密码错误"})
        else:
            # 2未有该账户，直接把账户密码存入user表
            user = User(username=username)
            user.set_password(password)
            user.save()
            ou=OauthUser(uid=weibo_uid,user=user,oauth_type="1")
            ou.save()
            payload = jwt_payload_handler(user)
            token = jwt_encode_handler(payload)
            data = jwt_response_payload_handler(token, user)
            data['type'] = "0"  # 指定账户已绑定
            return Response({"codo": 200, "msg": "登陆成功", "data": data})

